//
//  HWApnPushManager.m
//  嗨文
//
//  Created by 响当当 on 6/27/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "HWApnPushManager.h"
#import <UMCommon/UMCommon.h>

#define kApi_Appkey @"5d1476354ca3578cd30001f2"

static HWApnPushManager *_manager = nil;

@interface HWApnPushManager()<UNUserNotificationCenterDelegate>


@end

@implementation HWApnPushManager

/**
 *   推送/分享单例
 */
+ (HWApnPushManager *)manager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!_manager) {
            _manager = [[HWApnPushManager alloc]init];
            if (@available(iOS 10.0, *)) {
                [UNUserNotificationCenter currentNotificationCenter].delegate = (id)_manager;
            } else {
                // Fallback on earlier versions
            }
        }
    });
    return _manager;
}

/**
 *   初始化推送配置
 */
- (void)resetAPNsPushWithLaunchOptions:(NSDictionary * )launchOptions
{
    //开发者需要显式的调用此函数，日志系统才能工作
    [UMConfigure initWithAppkey:kApi_Appkey channel:@"App Store"];
    [UMConfigure setLogEnabled:false];
    [UMessage setBadgeClear:false];
    [UMessage openDebugMode:false];
    // Push's basic setting
    UMessageRegisterEntity * entity = [[UMessageRegisterEntity alloc] init];
    //type是对推送的几个参数的选择，可以选择一个或者多个。默认是三个全部打开，即：声音，弹窗，角标
    entity.types = UMessageAuthorizationOptionBadge|UMessageAuthorizationOptionAlert|UMessageAuthorizationOptionSound;
    [UMessage openDebugMode:true];
    [UMessage registerForRemoteNotificationsWithLaunchOptions:launchOptions Entity:entity completionHandler:^(BOOL granted, NSError * _Nullable error) {
        // Push功能配置
        if (granted) {
            NSLog(@"可以推送");
        }else{
            NSLog(@"没有推送权限");
        }
    }];
}

-(void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler

{
    [UMessage setAutoAlert:NO];
    //统计点击数
    [UMessage didReceiveRemoteNotification:userInfo];
    if([[[UIDevice currentDevice] systemVersion]intValue] < 10)
    {
        [UMessage didReceiveRemoteNotification:userInfo];
        completionHandler(UIBackgroundFetchResultNewData);
    }
}

-(void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler{
    
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        //应用处于前台时的远程推送接受
        //关闭U-Push自带的弹出框
        [UMessage setAutoAlert:true];
        [UMessage didReceiveRemoteNotification:userInfo];
    }else{
        //应用处于前台时的本地推送接受
    }
    //当应用处于前台时提示设置，需要哪个可以设置哪一个
    completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}

-(void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void(^)())completionHandler{
    
    NSDictionary* userInfo = response.notification.request.content.userInfo;
    
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        
        //应用处于后台时的远程推送接受
        
        //（前台、后台）的消息处理
        [UMessage didReceiveRemoteNotification:userInfo];
        if(userInfo.count>0){
            //消息处理
            NSLog(@"跳转到你想要的");
        }
    }
    else{ //应用处于后台时的本地推送接受
    }
}



@end
